<template>
  <div class="app-container">
    <div v-if="showPage">
      <el-form ref="queryForm" :model="queryParams" :inline="true">
        <el-form-item label="图书编号" prop="ctrlNo">
          <el-input
            v-model="queryParams.ctrlNo"
            placeholder="请输入图书编号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="楼幢" prop="buildingId">
          <el-select
            v-model="queryParams.buildingId"
            placeholder="请选择楼幢"
            clearable
            size="small"
            @change="handlechangeBuilding"
          >
            <el-option
              v-for="dict in buildingArr"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="楼层" prop="mapId">
          <el-select
            v-model="queryParams.mapId"
            placeholder="请选择楼层"
            clearable
            size="small"
          >
            <el-option
              v-for="dict in floorArr"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="small"
            @click="handleQuery"
          >搜索</el-button>
          <el-button
            icon="el-icon-refresh"
            size="small"
            @click="resetQuery"
          >重置</el-button>
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="small"
            @click="handleAdd"
          >新增</el-button>
          <el-button
            icon="el-icon-upload2"
            type="warning"
            plain
            @click="handleUploadd"
          >批量导入</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" border :data="list" style="width: 100%">
        <el-table-column
          key="ctrlNo"
          label="图书编号"
          align="center"
          prop="ctrlNo"
          width="200"
        />
        <el-table-column
          v-for="item in dynamicColumns"
          :key="item.key"
          :label="item.label"
          align="center"
          :prop="item.key"
          min-width="120"
        >
          <template slot-scope="scope">
            <div v-for="(tig, tindex) in scope.row.ctrlInfo" :key="tindex">
              <span v-if="item.key === tig.key">{{ tig.val }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          key="mapName"
          label="关联楼层"
          align="center"
          prop="mapName"
        />
        <el-table-column
          fixed="right"
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          width="220"
        >
          <template slot-scope="scope">
            <el-button
              size="small"
              type="text"
              icon=""
              class="delcolor"
              @click="handleDel(scope.row)"
            >
              删除
            </el-button>
            <el-button
              size="small"
              type="text"
              icon=""
              @click="handleEdit(scope.row, 'edit')"
            >
              修改
            </el-button>
            <el-button
              size="small"
              type="text"
              icon=""
              @click="handleCatalogue(scope.row)"
            >
              资产目录
            </el-button>
            <el-button
              size="small"
              type="text"
              icon=""
              @click="handleEdit(scope.row, 'bind')"
            >
              更换楼层绑定
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page-sizes="[20, 40, 60, 80]"
        :page.sync="queryParams.page.current"
        :limit.sync="queryParams.page.size"
        @pagination="getListpagination"
      />
      <popCon
        :id="chosid"
        :type="chosetype"
        :visible.sync="show"
        :obj-target="objTarget"
        @close="close"
      />
      <uploadCon
        :type="'msg'"
        :visible.sync="uploadshow"
        @close="uploadclose"
      />
    </div>
    <router-view v-else />
  </div>
</template>

<script>
import pagination from '@/components/Pagination'
import popCon from './components/add.vue'
import uploadCon from './components/upload.vue'
import { booksList, deleteBooks } from '@/api/books'
import { getfloorArr, getbuildingArr } from '@/api/map'
import { formTemplate } from './components/formTemplate'
import { mergeObjects } from '@/utils/index'
export default {
  name: 'BooksMsg',
  components: {
    pagination,
    popCon,
    uploadCon
  },
  data() {
    const formTemplateData = formTemplate
    return {
      formTemplateData: formTemplateData,
      queryParams: {
        ctrlNo: '',
        mapId: '',
        buildingId: '',
        page: {
          searchCount: true, // 是否显示搜索数量
          current: 1, // 当前页
          size: 20
        }
      },
      loading: false,
      list: [],
      dynamicColumns: [],
      total: 0,
      show: false,
      chosid: null,
      chosetype: '',
      objTarget: '',
      floorArr: [],
      buildingArr: [],
      uploadshow: false,
      showPage: true
    }
  },
  mounted() {
    if (this.$route.path === '/books/msg') {
      this.showPage = true
    } else {
      this.showPage = false
    }
    this.requestfloorArr(0)
    this.requestBuildingArr()
    this.handleQuery()
  },
  methods: {
    requestfloorArr(id) {
      this.floorArr = []
      getfloorArr(id)
        .then((res) => {
          if (res.success) {
            const arr = res?.data ?? []
            if (arr.length > 0) {
              arr.map((item) => {
                const obj = {
                  label: item.mapName,
                  value: item.mapId
                }
                this.floorArr.push(obj)
              })
            }
          }
        })
        .catch((err) => {})
    },
    requestBuildingArr() {
      this.buildingArr = []
      getbuildingArr()
        .then((res) => {
          if (res.success) {
            const arr = res?.data ?? []
            if (arr.length > 0) {
              arr.map((item) => {
                const obj = {
                  label: item.buildingName,
                  value: item.buildingId
                }
                this.buildingArr.push(obj)
              })
            }
          }
        })
        .catch((err) => {})
    },
    handlechangeBuilding(val) {
      this.$set(this.queryParams, 'mapId', '')
      if (val) {
        this.requestfloorArr(val)
      } else {
        this.requestfloorArr(0)
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList(true)
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.resetForm("queryForm");
      this.queryParams = {
        ctrlNo: '',
        mapId: '',
        buildingId: '',
        page: {
          searchCount: true, // 是否显示搜索数量
          current: 1, // 当前页
          size: 20
        }
      }
      this.handleQuery()
    },
    /** 查询菜单列表 */
    getList(falge) {
      this.loading = true
      this.queryParams.page.searchCount = falge
      booksList(this.queryParams).then((res) => {
        this.loading = false
        if (res.success) {
          this.list = res?.data ?? []
          if (falge || falge === undefined) {
            this.total = res?.page?.total ?? 0
          }
          this.generateColumns()
          // if (this.list.length > 0 && Array.isArray(this.list[0].ctrlInfo)) {
          // let arr = mergeObjects(this.list[0].ctrlInfo);
          // }
        }
      })
    },
    /* 分页查询参数 */
    getListpagination() {
      this.getList(false)
    },
    generateColumns() {
      this.dynamicColumns = []
      this.formTemplateData.map((item) => {
        const obj = {
          label: item.label,
          key: item.prop
        }
        this.dynamicColumns.push(obj)
      })
    },
    handleAdd() {
      this.show = true
      this.chosid = null
      this.chosetype = 'add'
    },
    handleUploadd() {
      this.uploadshow = true
    },
    uploadclose(status) {
      this.uploadshow = false
      setTimeout(() => {
        if (status) {
          this.handleQuery()
        }
      }, 200)
    },
    close(status) {
      this.show = false
      this.chosid = null
      setTimeout(() => {
        if (status) {
          this.handleQuery()
        }
      }, 200)
    },
    handleCatalogue(row) {
      this.$router.push({
        name: 'catalogue',
        query: {
          ctrlId: row.ctrlId,
          ctrlNo: row.ctrlNo
        }
      })
    },
    handleDel(row) {
      const text = '确定删除该图书信息?'
      this.$confirm(text, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.requestDel(row.ctrlId)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    requestDel(ctrlId) {
      deleteBooks(ctrlId)
        .then((res) => {
          if (res.success) {
            this.handleQuery()
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          }
        })
        .catch((err) => {
          this.$message({
            type: 'error',
            message: '删除失败!'
          })
        })
    },
    handleEdit(row, type) {
      this.show = true
      this.chosid = row.ctrlId
      this.objTarget = JSON.stringify(row)
      this.chosetype = type
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
